<?php

class Application_Model_ElementKosariceMapper
{
    protected $_dbTable;

      public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_ElementKosarice');
        }
        return $this->_dbTable;
    }
    
        public function save(Application_Model_ElementKosarice $elementKosarice, $insert = false)
    {
        $data = array(
            'kosarica_sifra_kosarice' => $elementKosarice->getKosaricaSifraKosarice(),
            'kolicina'   => $elementKosarice->getKolicina(),
            'produkt_idprodukt' => $elementKosarice->getProdukt_idprodukt(),
        );

        try {
            $novElementKosarice = new Application_Model_ElementKosarice();
            if (!$insert) {
               // $where = $this->getDbTable()->getAdapter()->quoteInto('produkt_idprodukt = ?', $data['produkt_idprodukt']);

                $where = array('produkt_idprodukt = ?' => $data['produkt_idprodukt'], 'kosarica_sifra_kosarice = ?' => $elementKosarice->getKosaricaSifraKosarice());

                $this->getDbTable()->update($data, $where);
            } else {
                $this->getDbTable()->insert($data);
            }
        } catch (Exception $e) {
            echo "Error: ".$e->getMessage();
            die;
        }

    }
    public function find($id, Application_Model_ElementKosarice $elementKosarice)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();

        $elementKosarice->setKosaricaSifraKosarice($row->kosarica_sifra_kosarice)
            ->setKolicina($row->kolicina)
            ->setProdukt_idprodukt($row->produkt_idprodukt);
    }

    public function fetchRow($where) {
        return $this->getDbTable()->fetchRow($where);
    }
    
    
    public function fetchAll($where) {
        return $this->getDbTable()->fetchAll($where);
    }

    public function delete($idprodukt)
    {
        $strankaSession = new Zend_Session_Namespace('Store_Stranke');
        $kosaricaId = $strankaSession->kosaricaId;

        if ($kosaricaId) {

            $where = array(
                'produkt_idprodukt = ?' => $idprodukt,
                'kosarica_sifra_kosarice = ?' => $kosaricaId
            );

            return $this->getDbTable()->getAdapter()->delete('element_kosarice', $where);
        }
    }

    public function getAllElementiKosarice($where = null) {
        $elementKosariceArray = $this->fetchAll($where);
        $entries = array();
        foreach ($elementKosariceArray as $elementKosarice) {
            $entry = new Application_Model_ElementKosarice();
            $entry->setKolicina($elementKosarice['kolicina']);
            $entry->setKosaricaSifraKosarice($elementKosarice['kosarica_sifra_kosarice']);

            $entries[$elementKosarice['kosarica_sifra_kosarice']] = $entry;
        }
        return $entries;
    }

    public function update($data, $polje, $vrednost) {
        $where = array('kosarica_sifra_kosarice = ?' => $data['sifra_kosarice'],
                       'produkt_idprodukt = ?'=> $data['idprodukt']);

        return $this->getDbTable()->getAdapter()->update('element_kosarice', array($polje => $vrednost), $where);
    }
    
    public function fetchAllAsArrayK($where) {
        $result = array();
        $elementKosariceArray = $this->fetchAll($where);
        foreach ($elementKosariceArray as $elementKosarice) {
            $elementArr = array();

            $elementArr['kolicina'] = $elementKosarice['kolicina'];
            $elementArr['kosarica_sifra_kosarice'] = $elementKosarice['kosarica_sifra_kosarice'];
           $elementArr['produkt_idprodukt'] = $elementKosarice['produkt_idprodukt'];

            $result[] = $elementArr;
        }

        return $result;
    }
}

